package com.house.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.house.common.core.domain.entity.DictData;
import com.house.common.core.domain.entity.DictType;
import com.house.common.core.domain.entity.House;
import com.house.common.core.domain.entity.Role;
import com.house.common.utils.StringUtils;
import com.house.system.mapper.DictDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.house.system.mapper.HouseSupportMapper;
import com.house.system.domain.HouseSupport;
import com.house.system.service.IHouseSupportService;

/**
 * 房屋家具类Service业务层处理
 *
 * @author tc
 * @date 2022-02-04
 */
@Service
public class HouseSupportServiceImpl implements IHouseSupportService {

    @Autowired
    private HouseSupportMapper houseSupportMapper;

    @Autowired
    private DictDataMapper dictDataMapper;

    /**
     * 查询房屋家具类
     *
     * @param houseId 房屋家具类主键
     * @return 房屋家具类
     */
    @Override
    public HouseSupport selectHouseSupportByHouseId(Long houseId) {
        return houseSupportMapper.selectHouseSupportByHouseId(houseId);
    }

    /**
     * 查询房屋家具类列表
     *
     * @param houseSupport 房屋家具类
     * @return 房屋家具类
     */
    @Override
    public List<HouseSupport> selectHouseSupportList(HouseSupport houseSupport) {
        return houseSupportMapper.selectHouseSupportList(houseSupport);
    }

    /**
     * 新增房屋家具类
     *
     * @param houseSupport 房屋家具类
     * @return 结果
     */
    @Override
    public int insertHouseSupport(HouseSupport houseSupport) {
        return houseSupportMapper.insertHouseSupport(houseSupport);
    }

    /**
     * 修改房屋家具类
     *
     * @param houseSupport 房屋家具类
     * @return 结果
     */
    @Override
    public int updateHouseSupport(HouseSupport houseSupport) {
        return houseSupportMapper.updateHouseSupport(houseSupport);
    }

    /**
     * 批量删除房屋家具类
     *
     * @param houseIds 需要删除的房屋家具类主键
     * @return 结果
     */
    @Override
    public int deleteHouseSupportByHouseIds(Long[] houseIds) {
        return houseSupportMapper.deleteHouseSupportByHouseIds(houseIds);
    }

    /**
     * 删除房屋家具类信息
     *
     * @param houseId 房屋家具类主键
     * @return 结果
     */
    @Override
    public int deleteHouseSupportByHouseId(Long houseId) {
        return houseSupportMapper.deleteHouseSupportByHouseId(houseId);
    }

    /**
     * 获取该房屋已分配家具
     *
     * @param houseId 房屋编号
     * @return 结果
     */
    @Override
    public List<HouseSupport> selectAllocatedList(Long houseId) {

        HouseSupport houseSupport = new HouseSupport();
        houseSupport.setHouseId(houseId);
        //先获取当前房屋id具备的家具类
        List<HouseSupport> supports = selectHouseSupportList(houseSupport);

        //再获取所有的家具类
        List<HouseSupport> list = selectSupportAll();
        for (HouseSupport sup : list) {  // 所有的
            for (HouseSupport support : supports) {  //当前的
                if (support.getSupportName().equals(sup.getSupportName())) {
                    //如果房屋原本具备此家具,则拥有的角色在页面上显示为勾选状态
                    sup.setFlag(true);
                    break;
                }
            }
        }
        //返回所有的家具类
        return list;
    }

    /**
     * 查询所有家具
     */
    private List<HouseSupport> selectSupportAll() {

        List<HouseSupport> houseSupports = new ArrayList<>();
        //只选择房屋家具的字典
        List<DictData> dictDatas = dictDataMapper.selectDictDataByType("sys_house_support");
        for (DictData dictData : dictDatas) {
            HouseSupport houseSupport = new HouseSupport();
            houseSupport.setSupportName(dictData.getDictValue());
            houseSupports.add(houseSupport);
        }
        return houseSupports;
    }

    /**
     * 修改房屋配套家具
     *
     * @param houseId  房屋id
     * @param supports 家具名称数组
     */
    @Override
    public void insertAllotSupport(Long houseId, String[] supports) {
        //先删除相关的家具
        houseSupportMapper.deleteHouseSupportByHouseId(houseId);
        //再插入
        insertHouseSupport(houseId, supports);
    }

    /**
     * 根据房屋编号查询对于配套家具
     * @param houseId 房屋id
     * @return
     */
    @Override
    public List<HouseSupport> selectSupportsByHouseId(Long houseId) {
        return houseSupportMapper.selectAllocatedList(houseId);
    }

    /**
     * 新增房屋配套家具信息
     *
     * @param houseId  房屋编号
     * @param supports 家具名称数组
     */
    private void insertHouseSupport(Long houseId, String[] supports) {
        if (StringUtils.isNotNull(supports)) {
            List<HouseSupport> list = new ArrayList<>();
            for (String supportName : supports) {
                HouseSupport houseSupport = new HouseSupport();
                houseSupport.setHouseId(houseId);
                houseSupport.setSupportName(supportName);
                list.add(houseSupport);
            }
            if (list.size() > 0) {
                houseSupportMapper.batchAllotSupport(list);
            }
        }
    }
}
